package com.wing.web.config;

import com.wing.common.utils.LocalDateTimeUtils;
import com.wing.web.common.interceptor.LoginInterceptor;
import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.MethodParameter;
import org.springframework.mobile.device.DeviceHandlerMethodArgumentResolver;
import org.springframework.mobile.device.DeviceResolverHandlerInterceptor;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * MVC配置
 *
 * @author
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

	@Resource
	private LoginInterceptor loginInterceptor;
	// @Autowired
	// private HttpInterceptor httpInterceptor;

	/**
	 * 注册拦截器
	 * 	-- springboot1.5.x，会自己识别static、public、resources下静态资源，不会被拦截。
	 * 	-- springboot2.x之后，如果拦截/**所有请求，那么静态资源也会被拦截器拦截，需要自己排除。
	 * @param registry
	 */
	@Override
    public void addInterceptors(InterceptorRegistry registry) {

        // 拦截所有http请求
		// registry.addInterceptor(httpInterceptor).addPathPatterns("/**");

		// 注册设备来源解析拦截器
        registry.addInterceptor(deviceResolverHandlerInterceptor());
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        // 在方法中添加当前Device参数
        argumentResolvers.add(deviceHandlerMethodArgumentResolver());
        // 增加 {@link LocalDateTime} 的自定义参数解析器
        argumentResolvers.add(new LocalDateArgumentResolverHandler());
    }

    @Bean
    public DeviceResolverHandlerInterceptor
    deviceResolverHandlerInterceptor() {
        return new DeviceResolverHandlerInterceptor();
    }

    @Bean
    public DeviceHandlerMethodArgumentResolver
    deviceHandlerMethodArgumentResolver() {
        return new DeviceHandlerMethodArgumentResolver();
    }

    public static class LocalDateArgumentResolverHandler implements HandlerMethodArgumentResolver {

        @Override
        public boolean supportsParameter(MethodParameter parameter) {
            return parameter.getParameterType().equals(LocalDateTime.class);
        }

        @Override
        public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
                                      NativeWebRequest webRequest, WebDataBinderFactory binderFactory) {
            String param = webRequest.getParameter(Objects.requireNonNull(parameter.getParameterName()));
            if (StringUtils.isEmpty(param)) {
                return null;
            }
            return LocalDateTimeUtils.convert(param.trim());
        }
    }
}